<template>
  <div class="product-detail">
    <div v-if="loading" class="loading">
      <el-skeleton :rows="10" animated />
    </div>
    <div v-else-if="product" class="product-container">
      <div class="product-header">
        <div class="product-image">
          <el-image :src="product.thumbnail" fit="cover">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </div>
        <div class="product-info">
          <h1>{{ product.name }}</h1>
          <div class="price">¥{{ product.price.toFixed(2) }}</div>
          <div class="sales">月销 {{ product.sales || 0 }}</div>
          <div class="merchant">所属商家: {{ product.merchantName }}</div>
          <div class="actions">
            <el-input-number v-model="quantity" :min="1" :max="99" size="small"></el-input-number>
            <el-button type="primary" @click="addToCart">加入购物车</el-button>
            <el-button type="danger" @click="buyNow">立即购买</el-button>
          </div>
        </div>
      </div>
      <div class="product-tabs">
        <el-tabs v-model="activeTab">
          <el-tab-pane label="商品详情" name="detail">
            <div v-html="product.description" class="product-description"></div>
          </el-tab-pane>
          <el-tab-pane label="商品评价" name="reviews">
            <div class="empty-reviews" v-if="!reviews.length">
              暂无评价
            </div>
            <div v-else class="review-list">
              <div v-for="(review, index) in reviews" :key="index" class="review-item">
                <div class="review-header">
                  <span class="username">{{ review.username }}</span>
                  <el-rate v-model="review.rating" disabled></el-rate>
                  <span class="time">{{ review.createdTime }}</span>
                </div>
                <div class="review-content">{{ review.content }}</div>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <div v-else class="not-found">
      商品不存在或已下架
    </div>
  </div>
</template>

<script>
import { getProductById } from '@/api/product'

export default {
  name: 'ProductDetail',
  data() {
    return {
      productId: null,
      product: null,
      loading: true,
      activeTab: 'detail',
      quantity: 1,
      reviews: [] // 示例数据，实际应从API获取
    }
  },
  created() {
    this.productId = this.$route.params.id
    this.fetchProductDetail()
  },
  methods: {
    fetchProductDetail() {
      this.loading = true
      getProductById(this.productId)
        .then(response => {
          this.product = response.data
          // 模拟获取商品评价
          this.fetchReviews()
        })
        .catch(error => {
          console.error('获取商品详情失败:', error)
          this.$message.error('获取商品详情失败')
        })
        .finally(() => {
          this.loading = false
        })
    },
    fetchReviews() {
      // 模拟获取评价数据
      // 实际开发中应该从API获取
      this.reviews = []
    },
    addToCart() {
      if (!this.$store.getters.isLogin) {
        this.$message.warning('请先登录')
        this.$router.push('/login?redirect=' + this.$route.fullPath)
        return
      }
      
      this.$store.dispatch('addToCart', {
        id: this.product.id,
        merchantId: this.product.merchantId,
        name: this.product.name,
        price: this.product.price,
        quantity: this.quantity
      })
      
      this.$message.success('已加入购物车')
    },
    buyNow() {
      if (!this.$store.getters.isLogin) {
        this.$message.warning('请先登录')
        this.$router.push('/login?redirect=' + this.$route.fullPath)
        return
      }
      
      // 添加到购物车并跳转到结算页面
      this.$store.dispatch('addToCart', {
        id: this.product.id,
        merchantId: this.product.merchantId,
        name: this.product.name,
        price: this.product.price,
        quantity: this.quantity
      })
      
      this.$router.push('/checkout')
    }
  }
}
</script>

<style scoped>
.product-detail {
  padding: 20px;
}

.loading {
  padding: 20px;
}

.product-container {
  background: #fff;
  border-radius: 4px;
  overflow: hidden;
}

.product-header {
  display: flex;
  padding: 20px;
}

.product-image {
  width: 400px;
  height: 400px;
  margin-right: 30px;
}

.product-info {
  flex: 1;
}

.product-info h1 {
  margin-top: 0;
  margin-bottom: 20px;
  font-size: 24px;
}

.price {
  font-size: 28px;
  color: #f56c6c;
  margin-bottom: 15px;
}

.sales, .merchant {
  margin-bottom: 15px;
  color: #606266;
}

.actions {
  margin-top: 30px;
}

.actions .el-button {
  margin-left: 15px;
}

.product-tabs {
  padding: 20px;
}

.product-description {
  min-height: 300px;
}

.empty-reviews {
  color: #909399;
  text-align: center;
  padding: 30px 0;
}

.review-list {
  margin-top: 15px;
}

.review-item {
  border-bottom: 1px solid #ebeef5;
  padding: 15px 0;
}

.review-header {
  margin-bottom: 10px;
  display: flex;
  align-items: center;
}

.username {
  margin-right: 10px;
  font-weight: bold;
}

.time {
  margin-left: 10px;
  color: #909399;
  font-size: 12px;
}

.review-content {
  line-height: 1.6;
}

.not-found {
  text-align: center;
  padding: 50px 0;
  font-size: 18px;
  color: #909399;
}
</style> 